package me.stone.stanimeclient.core.data.domain.fan

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import me.stone.stanimeclient.core.common.di.IoDispatcher
import me.stone.stanimeclient.core.data.domain.FlowUseCase
import me.stone.stanimeclient.core.data.domain.Result
import me.stone.stanimeclient.core.data.domain.data.ListUpdateDataRepository
import me.stone.stanimeclient.core.model.FanDailyUpdateItem
import me.stone.stanimeclient.core.model.FanWeekUpdateData
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
open class LoadDailyDataCase @Inject constructor(
    private val repository: ListUpdateDataRepository,
    @IoDispatcher private val dispatcher: CoroutineDispatcher
) : FlowUseCase<Unit, Map<Int, List<FanDailyUpdateItem>>>(dispatcher) {
    override fun execute(params: Unit): Flow<Result<Map<Int, List<FanDailyUpdateItem>>>> {
        return flow {
            emit(Result.Loading)
            val data = repository.listUpdateData()
            val map = mutableMapOf<Int, List<FanDailyUpdateItem>>()
            data.onEach { map[it.weekday] = it.updateList }
            emit(Result.Success(map))
        }
    }
}